package com.LeetCode.Hash;

import org.junit.Test;

import java.util.HashMap;

/**
 * 验证外形词语典
 */
public class CheckUFOWord {

    public boolean isAlienSorted(String[] words, String order) {
        int[] index = new int[26];

        for(int i = 0; i < order.length(); i++){
            index[order.charAt(i) - 'a'] = i;
        }

        search : for(int i = 0; i < words.length - 1; i++){
            String word1 = words[i];
            String word2 = words[i+1];

            for(int k = 0; k < Math.min(word1.length(), word2.length()) ; k++){
                if(word1.charAt(k) != word2.charAt(k)){
                    if(index[word1.charAt(k)-'a'] > index[word2.charAt(k) - 'a'])
                        return false;
                    continue search;
                }
            }

            //到这一步可以说明 word1 的长度等于 word2
            if(word1.length() > word2.length())
                return false;
        }
        return true;
    }

    @Test
    public void test(){
        String[] strs1 = {"hello","leetcode"};
        String order1 = "hlabcdefgijkmnopqrstuvwxyz";
        System.out.println(isAlienSorted(strs1,order1));

        String[] strs2 = {"word","world","row"};
        String order2 = "worldabcefghijkmnpqstuvxyz";
        System.out.println(isAlienSorted(strs2,order2));
    }

}
